﻿using Blm.Utils.Extensions;
using Newtonsoft.Json.Linq;
using System;
using VisionCloud.Domain.Common;
using VisionCloud.Utility;

namespace VisionCloud.Api.Common
{
    /// <summary>
    /// 模型验证
    /// </summary>
    public static class ModelValidate
    {
        /// <summary>
        /// 获取值 ,同时验证值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">参数集合</param>
        /// <param name="property"></param>
        /// <param name="isRequire">是否为必须</param>
        /// <param name="defaultVal">非必须情况,传入值为空时,指定的默认值</param>
        /// <returns></returns>
        public static T GetAndCheck<T>(this JObject obj, string property, bool isRequire = true, object defaultVal = null)
        {
            var field = obj.GetValue(property, StringComparison.OrdinalIgnoreCase);
            var value = field.NullToString();
            var isStringEmpty = string.IsNullOrWhiteSpace(value);

            if (isRequire && isStringEmpty)
                throw new OperationException(string.Format(StartupServer.CommonLocalizer["YB0001"].Value, property));

            if (!isRequire && isStringEmpty)
                if (defaultVal != null)
                    return (T)Convert.ChangeType(defaultVal, typeof(T));
                else
                    return default;

            switch (typeof(T).Name)
            {
                case "String":
                    return (T)Convert.ChangeType(value, typeof(string));

                case "Guid":
                    if (!Guid.TryParse(value, out var vg))
                        throw new OperationException(string.Format(StartupServer.CommonLocalizer["YB0002"].Value, property));
                    return (T)Convert.ChangeType(vg, typeof(Guid));

                case "Int":
                    if (!int.TryParse(value, out var vi))
                        throw new OperationException(string.Format(StartupServer.CommonLocalizer["YB0003"].Value, property));
                    return (T)Convert.ChangeType(vi, typeof(int));

                case "Int32":
                    if (!Int32.TryParse(value, out var vi32))
                        throw new OperationException(string.Format(StartupServer.CommonLocalizer["YB0003"].Value, property));
                    return (T)Convert.ChangeType(vi32, typeof(Int32));

                case "DateTime":
                    if (!DateTime.TryParse(value, out var vd))
                        throw new OperationException(string.Format(StartupServer.CommonLocalizer["YB0004"].Value, property));
                    return (T)Convert.ChangeType(vd, typeof(DateTime));

                case "Boolean":
                    if (!bool.TryParse(value, out var vb))
                        throw new OperationException(string.Format(StartupServer.CommonLocalizer["YB0005"].Value, property));
                    return (T)Convert.ChangeType(vb, typeof(bool));

                default:
                    throw new Exception(StartupServer.CommonLocalizer["YB0006"].Value);
            }
        }
    }
}